package com.eclipse.training.extensions.views;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.eclipse.training.models.BaseModelElement;

public class DescriptionSection extends AbstractTrainingPropertySection {

	private static final String TITLE = Messages.DescriptionSection_0;
	private static final String DESCRIPTION = Messages.DescriptionSection_1;
	private Object element;
	private Label text;

	public DescriptionSection() {
		title = TITLE;
		description = DESCRIPTION;
	}

	/**
	 * Build the section
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);

		TableWrapData layoutData = new TableWrapData(TableWrapData.FILL_GRAB,
				TableWrapData.FILL_GRAB);
		layoutData.colspan = 2;
		layoutData.maxWidth = 100;

		Composite composite = getWidgetFactory().createComposite(client,
				SWT.NONE);
		composite.setLayout(new TableWrapLayout());
		composite.setLayoutData(layoutData);

		text = getWidgetFactory().createLabel(composite, "", //$NON-NLS-1$
				SWT.WRAP | SWT.NO_FOCUS);
		text.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB,
				TableWrapData.FILL_GRAB));
	}

	/**
	 * Set the section input
	 */
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection) selection).getFirstElement();
		Assert.isTrue(input instanceof BaseModelElement);
		this.element = input;
	}

	/**
	 * Refresh input content
	 */
	@Override
	public void refresh() {
		text.setText(element.toString());
	}
}
